package com.ny.dao.impl;

import com.ny.dao.ProductDao;
import com.ny.entity.Product;
import com.ny.until.SqlConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    Connection connection = SqlConnection.getCon();

    public ProductDaoImpl() throws SQLException {
    }

    @Override
    public List<Product> findAllProduct() throws SQLException {
        String sql = "select * from products";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();

        List<Product> productList = new ArrayList<Product>();
        while (resultSet.next()) {
            Product product = new Product();
            product.setProduct_id(resultSet.getInt("product_id"));
            product.setName(resultSet.getString("name"));
            product.setPrice(resultSet.getDouble("price"));
            product.setStock(resultSet.getInt("stock"));
            product.setCategory(resultSet.getString("category"));
            product.setDescription(resultSet.getString("description"));
            productList.add(product);
        }
        return productList;
    }

    @Override
    public int addProduct(Product product) throws SQLException {
        String sql = "insert into products(name,price,stock,category,description) values(?,?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, product.getName());
        preparedStatement.setDouble(2, product.getPrice());
        preparedStatement.setInt(3, product.getStock());
        preparedStatement.setString(4, product.getCategory());
        preparedStatement.setString(5, product.getDescription());
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("添加成功");
        }
        return 0;
    }

    @Override
    public int deleteProduct(int productId) throws SQLException {
        String sql = "delete from products where product_id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, productId);
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("删除成功");
        }
    return 0;
    }

    @Override
    public int updateProduct(Product product) throws SQLException {
        String sql = "update products set name=?,price=?,stock=?,category=?,description=? where product_id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, product.getName());
        preparedStatement.setDouble(2, product.getPrice());
        preparedStatement.setInt(3, product.getStock());
        preparedStatement.setString(4, product.getCategory());
        preparedStatement.setString(5, product.getDescription());
        preparedStatement.setInt(6, product.getProduct_id());
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("修改成功");
        }
        return 0;
    }
}





